jQuery(function(){
	/////新增客户来访信息
	jQuery('.addBtn').on('click',function(){
		var url=window.rootPath+'/ui/customerVisiting/add';
		window.location = url;	
	});
	
	/////客户来访信息列表
	    var serverData = {};
	    serverData.key ='';
	    var serverTable;
	    jQuery(document).ready(function () {
	        serverTable = jQuery('#visitTable').DataTable({
	            "serverSide": true,
	            "ordering": false,
	            "ajax": {
	                "dataType": "json",
	                "type": "get",
	                "cache": false,
	                "url":window.rootPath+ '/ui/customerVisiting/getpage',
	                "data": function (d) {
	                    return $.extend({}, d, serverData);
	                }
	            },
	            columns: [
	                {
	                	data: null
	                },
	                {
	                    data: null,
	                    defaultContent: "",
	                    render: function (data, type, full, meta) {
	                    	var id=data.id
	    	            	var operate ='<input type="checkbox" class="checkchild">';
	    	            	return operate;

	                    }
	                },
	                {
	                    data: 'customer.customerCode'
	                },
	                {
	                    data: 'customer.customerName',
		                render:function(data,type,full,meta){
		                	if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
							}
						}
	                },
	                {
	                    data: 'visitingDate',
	                    render: function (data, type, full, meta) {
	                            if (data)
	                                return moment(data).format('YYYY/MM/DD');
	                            else
	                                return '';
	                     }
	                },
	                {
	                    data: 'project'
	                },
	                {
	                	data: 'contractAmount'
	                },
	                {
	                	data: 'development'
	                },
	                {
	                	data: 'customer.creditLevel'
	                },
	                {
	                	data: 'operator'
	                },
	                {
	                	data: 'prove'
	                },
	                {
	                	data: 'saleArea.superiorArea'
	                },
	                {
	                	data: 'memo',
		                render:function(data,type,full,meta){
		                	if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
							}
						}
	                }
	                ],
	            "drawCallback": function (oSettings) {
	                var k = 0;
	                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

	                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
	                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
	                    k++;
	                }
	            }
	        });
	        
	        

	    });
	    
	    ///编辑来访
	    jQuery('.editBtn').on('click', function () {
	       var selects=getSelect("#visitTable",1);
	 	   if(selects.length>1){
	 		   $.springLemonDialog({
	 		         content: '只能编辑一条记录',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		             $(this).closeDialog(modal);
	 		         }
	 		   }); 
	 	   }
	 	   else if(selects.length==0){
	 		   $.springLemonDialog({
	 		         content: '请选择要编辑的记录',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		             $(this).closeDialog(modal);
	 		         }
	 		   }); 
	 	   }else if(selects.length==1){
	 		  var visitId=selects[0].id;
	 		  window.location = window.rootPath+"/ui/customerVisiting/edit"+"?visitId="+visitId;  
	 	   }
	 	   
	 	});
	    
	  ///查看来访
	    jQuery('.seeBtn').on('click', function () {
	       var selects=getSelect("#visitTable",1);
	 	   if(selects.length>1){
	 		   $.springLemonDialog({
	 		         content: '只能查看一条记录',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		             $(this).closeDialog(modal);
	 		         }
	 		   }); 
	 	   }
	 	   else if(selects.length==0){
	 		   $.springLemonDialog({
	 		         content: '请选择要查看的记录',// 确认文本
	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	 		             $(this).closeDialog(modal);
	 		         }
	 		   }); 
	 	   }else if(selects.length==1){
	 		  var visitId=selects[0].id;
	 		  window.location = window.rootPath+"/ui/customerVisiting/see"+"?visitId="+visitId;  
	 	   }
	 	   
	 	});
	    //////删除来访//////
	    jQuery('.delBtn').on('click', function () {
	 	   var selects=getSelect("#visitTable",1);
	 	   var url=window.rootPath+"/ui/customerVisiting/deleteVisit";
	 	   delSelect(selects,url,serverTable) 
	 	});
	    
	    ///来访查询
	    jQuery('.searchBtn').on('click',function(){
	    	var key=jQuery('#keyword').val();
	    	serverData.key=key;
	    	serverTable.draw();
        });
        //撤销查询
        jQuery('.resetBtn').on("click",function()
        {
            jQuery('#keyword').val('');
            serverData.key ='';
            serverTable.draw();
        });
        
  ///////列表中的全选
        $("#checkAll").click(function(){
        	var check = $(this).prop("checked");
            $(".checkchild").prop("checked", check);
    	});
})

	
	
